package com.fanfan.controller;

import com.fanfan.enums.AppHttpCodeEnum;
import com.fanfan.exception.SystemException;
import com.fanfan.pojo.Trade;
import com.fanfan.pojo.domain.ResponseResult;
import com.fanfan.service.TradeService;
import com.fanfan.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/tradeList")
public class TradeController {
    @Autowired
    TradeService tradeService;

    @GetMapping
    public ResponseResult tradeList(Integer pageNum, Integer pageSize,Trade trade){
        return  tradeService.tradeList(pageNum,pageSize,trade);
    }
    @PostMapping
    public ResponseResult addTrade(@RequestBody Trade trade){
        return tradeService.addTrade(trade);
    }
    @PutMapping
    public ResponseResult updateTrade(@RequestBody Trade trade){
        tradeService.updateById(trade);
        return ResponseResult.okResult();
    }
    @DeleteMapping("/{id}")
    public ResponseResult delTrade(@PathVariable Long id){
        tradeService.getBaseMapper().deleteById(id);
        return ResponseResult.okResult();
    }
    @GetMapping("/{id}")
    public ResponseResult getTradeDetail(@PathVariable Long id){
        Trade trade = tradeService.getBaseMapper().selectById(id);
        return ResponseResult.okResult(trade);
    }
    @PutMapping("/changeStatus")
    public ResponseResult changeTradeStatus(@RequestBody Trade trade){
        tradeService.getBaseMapper().updateById(trade);
        return ResponseResult.okResult();
    }
    @PutMapping("/changeIsTop")
    public ResponseResult changeTradeIsTop(@RequestBody Trade trade){
        if (SecurityUtils.isAdmin()){
        tradeService.getBaseMapper().updateById(trade);
        return ResponseResult.okResult();
        }else {
            throw new SystemException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

    }
}
